package com.gshrms.recruitment.controller;

import com.gshrms.common.annotation.Log;
import com.gshrms.common.core.controller.BaseController;
import com.gshrms.common.core.domain.AjaxResult;
import com.gshrms.common.core.domain.entity.SysDictData;
import com.gshrms.common.core.domain.entity.SysUser;
import com.gshrms.common.enums.BusinessType;
import com.gshrms.common.utils.StringUtils;
import com.gshrms.common.utils.file.FileUploadUtils;
import com.gshrms.common.utils.uuid.IdUtils;
import com.gshrms.recruitment.entity.RecruitmentApplicants;
import com.gshrms.recruitment.entity.RecruitmentInfoPosition;
import com.gshrms.recruitment.entity.RecruitmentInterview;
import com.gshrms.recruitment.entity.param.RecruitmentApplicantsParam;
import com.gshrms.recruitment.entity.param.RecruitmentFeedbackParam;
import com.gshrms.recruitment.entity.param.RecruitmentReportParam;
import com.gshrms.recruitment.service.IRecruitmentApplicantsService;
import com.gshrms.setting.domain.StaffInfoTable;
import com.gshrms.system.mapper.SysUserMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 应聘者信息表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2025-03-31
 */
@RestController
@Api(value = "应聘", tags = "应聘")
@Slf4j
@RequestMapping("/recruitment")
public class RecruitmentApplicantsController extends BaseController {

    @Autowired
    IRecruitmentApplicantsService recruitmentApplicantsService;
    @Autowired
    SysUserMapper userMapper;

    @ApiOperation("投递简历")
    @PostMapping("/submitResume")
    @Log(title = "投递简历", businessType = BusinessType.INSERT)
    public AjaxResult submitResume(@Validated @RequestBody RecruitmentApplicantsParam param) {
        Long userId = getUserId();
        //判断应聘者不可同时投递其他岗位
        Integer count = recruitmentApplicantsService.isRepeat(userId);
        if (count > 0) {
            throw new RuntimeException("不可重复投递");
        }
        RecruitmentApplicants recruitmentApplicants = new RecruitmentApplicants();
        BeanUtils.copyProperties(param, recruitmentApplicants);
        recruitmentApplicants.setApplyUserId(userId);
        recruitmentApplicants.setId(IdUtils.randomUUID());
        recruitmentApplicants.setSubmitTime(new Date());
        recruitmentApplicants.setCreateTime(new Date());
        recruitmentApplicants.setInterviewPhase("新增应聘");
        recruitmentApplicants.setApplyUserId(userId);
        recruitmentApplicants.setRecruitmentInfoId(param.getRecruitmentId());
        recruitmentApplicants.setRecruitmentInfoName(param.getRecruitmentName());
        if (StringUtils.isNotBlank(param.getSignImage()) && !param.getSignImage().startsWith("http")) {
            //签字上传图片
            FileUploadUtils fileUploadUtils = new FileUploadUtils();
            try {
                String SignUrl = fileUploadUtils.saveBase64Image(param.getSignImage());
                recruitmentApplicants.setSignImage(SignUrl);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        recruitmentApplicantsService.insert(recruitmentApplicants);

        return success();
    }

    @ApiOperation("招聘职位")
    @GetMapping("/vacancy")
    public AjaxResult<List<RecruitmentInfoPosition>> vacancy() {
        List<RecruitmentInfoPosition> list = recruitmentApplicantsService.vacancy();
        Long userId = getUserId();
        List<String> ids = recruitmentApplicantsService.myVacancy(userId);
        HashMap<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("ids", ids);
        return AjaxResult.success(result);
    }

    @ApiOperation("我的应聘-未读消息")
    @GetMapping("/notReadNum")
    public AjaxResult notReadNum() {

        Long userId = getUserId();
        Integer sendNum = recruitmentApplicantsService.notReadNum(userId);
        return success(sendNum);
    }

    @ApiOperation("我的应聘")
    @GetMapping("/myApplication")
    public AjaxResult<List<RecruitmentInterview>> myApplication() {

        Long userId = getUserId();
        List<RecruitmentInterview> list = recruitmentApplicantsService.myApplication(userId);
        //清空未读消息
        recruitmentApplicantsService.updateReadState(userId);
        return AjaxResult.success(list);
    }

    @ApiOperation("岗位的硬性要求")
    @GetMapping("/strictRequirement/{positionId}")
    public AjaxResult<RecruitmentInfoPosition> strictRequirement(@PathVariable String positionId) {

        RecruitmentInfoPosition recruitmentInfoPosition = recruitmentApplicantsService.selectById(positionId);
        recruitmentInfoPosition.setList(recruitmentApplicantsService.strictRequirement(positionId));
        return AjaxResult.success(recruitmentInfoPosition);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type")
    @ApiOperation("获取数据字典")
    public AjaxResult dictType() {
        List<SysDictData> data = recruitmentApplicantsService.selectDictDataByType();
        return success(data);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/tableConfig")
    @ApiOperation("获取子表和字段")
    public AjaxResult<StaffInfoTable> tableConfig() {
        List<StaffInfoTable> data = recruitmentApplicantsService.selectTable();
        return success(data);
    }


    @ApiOperation("邀请反馈 同意 拒绝")
    @PostMapping("/feedback")
    @Log(title = "面试邀请反馈", businessType = BusinessType.UPDATE)
    public AjaxResult feedback(@Validated @RequestBody RecruitmentFeedbackParam param) {
        recruitmentApplicantsService.feedback(param);
        return success();
    }

    @ApiOperation("上传体检报告")
    @PostMapping("/uploadReport")
    @Log(title = "上传体检报告", businessType = BusinessType.UPDATE)
    public AjaxResult uploadReport(@Validated @RequestBody RecruitmentReportParam param) {
        recruitmentApplicantsService.uploadReport(param);
        return success();
    }

    @ApiOperation("删除体检报告")
    @GetMapping("/delReport")
    @Log(title = "上传体检报告", businessType = BusinessType.DELETE)
    public AjaxResult delReport(@Validated @RequestBody RecruitmentReportParam param) {
        recruitmentApplicantsService.delReport(param);
        return success();
    }

}
